<?php
	// Handle redirection for a mobile device
	require_once("mobile_device_detect.php");
	// TODO: Change for non-development version
	mobile_device_detect(true,true,true,true,true,true,true,'https://cs340.pugetsound.edu/dev/mobile/',false);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>iRoomMonitor</title>
<!-- IE8 compatability fix -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<!-- the 'link' method for css inclusion is supported by most browsers -->
<link type="text/css" href="reset.css" rel="stylesheet" />
<link type="text/css" href="style.css" rel="stylesheet" />
<?php
	
	// We use session variables, start a session
	session_start();

	// Store username in variable
	$name = $_SESSION["username"];

	// Default theme to Royal Panda
	$theme = "style_royal_panda.css";

	// If theme value supplied in POST, store it in session so we don't lose it.
	if(isset($_POST['theme']))
	{
		$_SESSION['theme'] = $_POST['theme'];

		// Store the value in a cookie that lasts "forever"
		setcookie($name.':theme', $_SESSION['theme'], 2147483647);
	}

	// If the cookie is set...
	else if(isset($_COOKIE[$name.':theme']) )
	{
		$_SESSION['theme'] = $_COOKIE[$name.':theme'];
	}
	else
	{ 
		// If not, default to panda
		$_SESSION['theme'] = "style_royal_panda.css";
	}

	// By now, a theme should be set in the session variable.
	$theme = $_SESSION['theme'];

	// Link to theme
	echo '<link type="text/css" href="'.$theme.'" rel="stylesheet" />';
?>
</head>

<?php
	// MySQL connection information
	include_once("/var/mysql/mysql_connect.php");

	// Our handy function library
	include_once("functions.php");

	// Construct the whitelist of approved pages the user may visit
	$whitelist = array("loginform","home","privacy","help", "register");
	
	// Append any pages that are available for logged in users
	if(isset($_SESSION["loggedIn"]))
	{
	 	$whitelist[] = "recentdata";					// Your Room
		$whitelist[] = "timeforms";						// Your History
		$whitelist[] = "sensormanagement";		// Your Sensors
		$whitelist[] = "settings";						// Your Account
	}
?>

<body>
<div id="wrap">
  <div id="header"><h1>iRoomMonitor</h1></div>
  <div id="nav">
    <?php include("navbar.php"); ?>
  </div>
	<div id="container">
		<div id="main">
			<?php
				// Test if there is a page (p) parameter set, and if it is in the whitelist 
				if(isset($_GET["p"]) && in_array($_GET["p"],$whitelist))
				{
					// Include the page here
					include("includes/".$_GET["p"].".php");
				}
				else
				{
					// Default to home if unapproved page visit
					header("Location:?p=home");
				}
			?>
		</div>
		<div id="sidebar">
			<?php include("sidebar.php"); ?>
		</div>
	</div>
  <div id="footer">
  	<?php include("footer.php"); ?>
  </div>
</div>
</body>

<?php
	// Close the MySQL database connection
	mysqli_close($database);
?>

</html>
